﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using OutdoorLib.BaseEntity.WS;
using OutdoorLib.BaseEntity.Dictionary;


namespace Grishko.UI
{


    /// <summary>
    /// Summary description for TreeContextMenu
    /// </summary>
    public class TreeContextMenu : Control
    {
        public class MenuContextValue
        {
            

            public static MenuContextValue Create(string s)
            {
                
                string[] p = s.Split("&".ToCharArray()[0]);
                MenuContextValue res = new MenuContextValue() { NodeId = p[0], FolderID = p[1], DictionaryID = p[2], ClientAddClick = p[3], ClientRemoveClick =p[4]};
                return res;
            }

            public override string ToString()
            {
                string[] s = { NodeId, FolderID, DictionaryID, ClientAddClick, ClientRemoveClick };
                return string.Format("'{0}&{1}&{2}&{3}&{4}'", s);
                
            }

            public MenuContextValue() { }
            public string NodeId{get;set;}
            public string FolderID{get;set;}
            public string DictionaryID{get;set;}
            public string ClientAddClick { get; set; }
            public string ClientRemoveClick { get; set; }

        }

        MenuContextValue _params = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="NodeId">Обрабатываемая нода</param>
        public TreeContextMenu(MenuContextValue Params)
        {
            _params = Params;            
        }
        protected override void Render(HtmlTextWriter writer)
        {
            // Поднимаем справочник
            wsDictionary ws = new wsDictionary();
            //TreeDictionaryItem d = ws.GetTreeDictionary(_params.FolderID, _params.DictionaryID );

            //TreeDictionaryItem f = (TreeDictionaryItem)d.Find(_params.NodeId);

            Table t = new Table(){Width =new Unit ("120px")};
            t.Rows.Add(GetMenuRow("MyContextMenu_ADD", _params.ClientAddClick , "Добавить"));
            t.Rows.Add(GetMenuRow("MyContextMenu_REMOVE", _params.ClientRemoveClick , "Удалить"));  
            t.RenderControl(writer);

        }

        private TableRow GetMenuRow(string css,string OnClick,string Text)
        {
            TableRow r = new TableRow();
            r.Attributes.Add("onmouseover", "GrdItemOnMouseOver(this);");
            r.Attributes.Add("onmouseout", "GrdItemOnMouseOut(this);");
            
            TableCell Cell = new TableCell() { Width = new Unit("20px") };
            Cell.CssClass = css;
            Cell.Attributes.Add("onClick", OnClick); 
            r.Cells.Add(Cell);

            

            Cell = new TableCell() { Text = Text };
            Cell.Attributes.Add("onClick", OnClick); 
            r.Cells.Add(Cell);
            return r;
        }
    }
}
